package com.example.parkingsharing.ui.navigation

import androidx.compose.foundation.layout.*
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.parkingsharing.data.BottomItem
import com.example.parkingsharing.data.mBottomTabItems
import com.example.parkingsharing.ui.theme.ParkingSharingTheme
import com.google.accompanist.insets.navigationBarsHeight

@Composable
fun BottomNavBar(
    selectedPosition: Int,
    bottomItems: List<BottomItem>,
    onItemSelected: (position: Int, item: BottomItem) -> Unit
){
    Column {
        BottomNavigation(
            backgroundColor = MaterialTheme.colors.surface,
            modifier = Modifier
                .fillMaxWidth(),
        ) {
            bottomItems.forEachIndexed { index, item ->
                val isSelected = selectedPosition == index
                BottomNavigationItem(
                    selected = isSelected,
                    onClick = {
                        onItemSelected.invoke(index, item)

                    },
                    icon = {
                        val modifier = if(isSelected){
                            Modifier.scale(1.2f)
                        }else{
                            Modifier.scale(1f)
                        }
                        NavigationIcon(
                            res = item.Res,
                            isSelected = isSelected,
                            modifier = modifier)
                    },
                    label = {
                        val labelStyle = if (isSelected) {
                            TextStyle(
                                fontWeight = FontWeight.Medium,
                                color = MaterialTheme.colors.onSurface.copy(alpha = 0.8f),
                                fontSize = 12.sp
                            )
                        } else {
                            TextStyle(
                                fontWeight = FontWeight.Normal,
                                color = MaterialTheme.colors.onSurface.copy(alpha = 0.4f),
                                fontSize = 10.sp
                            )
                        }
                        Text(
                            text = item.label,
                            style = labelStyle,
                        )
                    },
                    modifier = Modifier
                        .fillMaxSize()
//                        .clickable(
//                            onClick = {},
//                            indication = null,
//                            interactionSource = remember {
//                                // 默认 Modifier.clickable() 点击事件自带点击水波纹效果；
//                                // 使用 indication = null, interactionSource = remember{} 取消
//                                MutableInteractionSource()
//                            }
//                        )
                )
            }
        }
        Spacer(modifier = Modifier.navigationBarsHeight())
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewBottomBarWidget() {
    ParkingSharingTheme {
        BottomNavBar(selectedPosition = 0, bottomItems = mBottomTabItems) {
            index, item ->
        }
    }
}